package com.dman.multi.cmd;

import java.util.Calendar;

import org.apache.pivot.collections.List;
import org.apache.pivot.wtk.TreeView;
import org.apache.pivot.wtk.content.TreeNode;

import com.dman.multi.core.Download;

public class GUIHistoryCommand implements Command {
	
	private enum Filter{
		TODAY(1), YESTERDAY(2), LAST_WEEK(7), LAST_MONTH(30), LAST_YEAR(365);
		
		private int numDays = 0;
		
		private Filter(int numDays){
			this.numDays = numDays;
		}
		
		public int getNumDays() {
			return numDays;
		}
	}

	private TreeView treeView;
	private List<Download> downloadList;
	
	public GUIHistoryCommand(List<Download> downloadList, TreeView treeView) {
		this.treeView = treeView;
		this.downloadList = downloadList;
	}

	@Override
	public void execute() {
		TreeNode tn = (TreeNode) treeView.getSelectedNode();
		String text = tn.getText().replace(' ', '_').toUpperCase();
		Filter filterType = Filter.valueOf(text);
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, -1 * filterType.getNumDays());
		Calendar now = Calendar.getInstance();
		Calendar downloadStart = null;
		for(Download d: downloadList){
			d.setVisible(false);
			downloadStart = d.getStartDate();
			if(downloadStart.after(cal) && downloadStart.before(now))
				d.setVisible(true);
		}
	}

}
